//
//  RegisterViewController.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "LTView.h"
#import <AVOSCloud/AVOSCloud.h>

@interface RegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) RegisterView *registerView;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addBar];
    [self addView];
    
}

#pragma mark - 添加bar
- (void)addBar
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnToLast:)];
    leftItem.tintColor = [UIColor colorWithHue:0.8 saturation:1 brightness:0.7 alpha:1];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(regist:)];
    rightItem.tintColor = [UIColor colorWithHue:0.8 saturation:1 brightness:0.7 alpha:1];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.navigationController.navigationBar setTranslucent:NO];
}

- (void)returnToLast:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 注册按钮
- (void)regist:(UIButton *)button
{
    if (self.registerView.userNameView.textField.text.length != 0 && self.registerView.passWordView.textField.text.length != 0 && self.registerView.surePassWordView.textField.text.length != 0 && self.registerView.addressView.textField.text.length != 0 && self.registerView.phoneNumberView.textField.text.length != 0) {
        // 说明没有空的
        if ([self.registerView.passWordView.textField.text isEqualToString:self.registerView.surePassWordView.textField.text]) {
           // 说明密码相同
            [self registDataWithName:self.registerView.userNameView.textField.text password:self.registerView.passWordView.textField.text address:self.registerView.addressView.textField.text phoneNumber:self.registerView.phoneNumberView.textField.text];
        } else {
            // 密码不同
            [self showAlertControllerAndDismissWithTitle:@"提醒" AndString:@"两次输入的密码不一样，请重新输入"];
            
        }
    } else {
        [self showAlertControllerAndDismissWithTitle:@"警告" AndString:@"内容不能为空!"];
    }
}

// 注册的方法
- (void)registDataWithName:(NSString *)userName password:(NSString *)password address:(NSString *)address phoneNumber:(NSString *)phoneNumber
{
    AVUser *user = [AVUser user];
    user.username = userName;
    user.password = password;
    user.email = address;
    user.mobilePhoneNumber = phoneNumber;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            switch (codeNum) {
                case 203:
                {
                    [self showAlertControllerAndDismissWithTitle:@"警告" AndString:@"该邮箱已经被占用"];
                    break;
                }
                    case 125:
                {
                    [self showAlertControllerAndDismissWithTitle:@"警告" AndString:@"邮箱格式不正确"];
                    break;
                }
                    case 127:
                {
                    [self showAlertControllerAndDismissWithTitle:@"警告" AndString:@"请输入正确的手机号"];
                    break;
                }
                    case 202:
                {
                    [self showAlertControllerAndDismissWithTitle:@"警告" AndString:@"该用户名已经被占用"];
                    break;
                }
                    case 211:
                {
                    [self showAlertControllerAndDismissWithTitle:@"警告" AndString:@"没有这个用户，请核对后登录"];
                }
                default:
                   
                   break;
                
                    
            }
        } else {
            if (succeeded) {
                NSLog(@"注册成功");
                [self showAlertControllerAndDismissWithTitle:@"通知" AndString:@"恭喜您注册成功"];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [self showAlertControllerAndDismissWithTitle:@"通知" AndString:@"没抱歉您没有注册成功"];
            }
        }
     
        
    }];
    
    
   
}

// 添加视图
- (void)addView
{
    self.registerView = [[RegisterView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.registerView.userNameView.textField.delegate = self;
    self.registerView.passWordView.textField.delegate = self;
    self.registerView.surePassWordView.textField.delegate = self;
    self.registerView.addressView.textField.delegate = self;
    self.registerView.phoneNumberView.textField.delegate = self;
    
    
    
    
    
    [self.view addSubview:_registerView];
    // 添加手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelResponse)];
    [self.registerView addGestureRecognizer:tapGes];
}

- (void)cancelResponse
{
    [self.registerView.userNameView.textField resignFirstResponder];
    [self.registerView.passWordView.textField resignFirstResponder];
    [self.registerView.surePassWordView.textField resignFirstResponder];
    [self.registerView.addressView.textField resignFirstResponder];
    [self.registerView.phoneNumberView.textField resignFirstResponder];

}

#pragma mark - textFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}



#pragma mark - 封装一个方法弹出提示框并在2秒后消失

- (void)showAlertControllerAndDismissWithTitle:(NSString *)title AndString:(NSString *)alertString
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:alertString preferredStyle:UIAlertControllerStyleAlert];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alertVC dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    });
    [self presentViewController:alertVC animated:YES completion:nil];
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
